package cn.ljx.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.ljx.entity.JsonReturn;
import cn.ljx.entity.TBUser;
import cn.ljx.service.TBPositionService;
import cn.ljx.util.BaseUtils;

@Controller
@RequestMapping("/Position")
public class PositionController {
	@Autowired
    private TBPositionService tbPositionService;
	
	//新增地点
	@ResponseBody
	@RequestMapping(value="/addposition",method=RequestMethod.POST)
    public JsonReturn addposition(@RequestParam Map<String,Object> map,HttpSession httpsession){
		JsonReturn jr = new JsonReturn();
		TBUser userSession = (TBUser) httpsession.getAttribute("tbuser");
		if(userSession!=null){
			try {
				map.put("id", BaseUtils.getUUID32());
				map.put("create_time", BaseUtils.getTime());
				map.put("username", userSession.getReal_name());
				map.put("state", "1");
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			int i = tbPositionService.addposition(map);
			if(i!=0){
				jr.setInfo("success");
			}else{
				jr.setInfo("false");
			}
		}else{
			jr.setInfo("false");
		}
		return jr;
	}
	
	//更新地点
	@ResponseBody
	@RequestMapping(value="/updateposition",method=RequestMethod.POST)
		public JsonReturn updateposition(@RequestParam Map<String,Object> map){
		JsonReturn jr = new JsonReturn();
		int i = tbPositionService.updateposition(map);
		if(i!=0){
			jr.setInfo("success");
		}else{
			jr.setInfo("false");
		}
		return jr;
	}
	
	//删除地点
	@ResponseBody
	@RequestMapping(value="/deleteposition",method=RequestMethod.POST)
		public JsonReturn deleteposition(String id){
		JsonReturn jr = new JsonReturn();
		int i = tbPositionService.deleteposition(id);
		if(i!=0){
			jr.setInfo("success");
		}else{
			jr.setInfo("false");
		}
		return jr;
	}
	
	//查找个人所有地点
	@ResponseBody
	@RequestMapping(value="/selectall",method=RequestMethod.POST)
		public JsonReturn selectall(@RequestParam Map<String,Object> map,HttpSession httpsession){
		JsonReturn jr = new JsonReturn();
		TBUser userSession = (TBUser) httpsession.getAttribute("tbuser");
		if(userSession!=null){
			map.put("username", userSession.getReal_name());
			List<Map<String,Object>> list = tbPositionService.selectall(map);
			
			if(list.size()>0){
				jr.setInfo("success");
				jr.setMaplist(list);
			}else{
				jr.setInfo("false");
			}
		}else{
			jr.setInfo("false");
		}
		return jr;
	}
	
	//查找某个地点
	@ResponseBody
	@RequestMapping(value="/selectbyid",method=RequestMethod.POST)
		public JsonReturn selectbyid(String id){
		JsonReturn jr = new JsonReturn();
		Map<String, Object> map = tbPositionService.selectbyid(id);
		if(map!=null){
			jr.setInfo("success");
			jr.setObject(map);
		}else{
			jr.setInfo("false");
		}
		return jr;
	}
}